# Emobot Startup Script (PowerShell)
# This script starts the emobot services locally

Write-Host "🤖 Emobot Startup Script" -ForegroundColor Green
Write-Host "======================================" -ForegroundColor Green

# Check if uv is available
if (-not (Get-Command uv -ErrorAction SilentlyContinue)) {
    Write-Host "❌ uv is not available. Please install uv first." -ForegroundColor Red
    Read-Host "Press Enter to exit"
    exit 1
}

Write-Host "✅ uv is available" -ForegroundColor Green

# Activate Python virtual environment (Windows specific)
Write-Host "🔧 Activating Python virtual environment..." -ForegroundColor Cyan
$venvActivateScript = Join-Path -Path $PSScriptRoot -ChildPath ".venv\Scripts\activate"
if (Test-Path $venvActivateScript) {
    . $venvActivateScript
    Write-Host "✅ Virtual environment activated" -ForegroundColor Green
} else {
    Write-Host "⚠️ Virtual environment not found at $venvActivateScript" -ForegroundColor Yellow
    Write-Host "⚠️ Continuing without activating virtual environment" -ForegroundColor Yellow
}

# Start Docker Compose services
Write-Host ""
Write-Host "🐳 Starting Docker Compose services..." -ForegroundColor Cyan

# Start main docker-compose.yml
$mainDockerCompose = Join-Path -Path $PSScriptRoot -ChildPath "docker-compose.yml"
if (Test-Path $mainDockerCompose) {
    Write-Host "📦 Starting services from docker-compose.yml" -ForegroundColor Cyan
    docker-compose -f $mainDockerCompose up -d
    Write-Host "✅ Main Docker services started" -ForegroundColor Green
} else {
    Write-Host "⚠️ docker-compose.yml not found at $mainDockerCompose" -ForegroundColor Yellow
}

# Start weaviate docker-compose-weaviate.yml
$weaviateDockerCompose = Join-Path -Path $PSScriptRoot -ChildPath "docker-compose-weaviate.yml"
if (Test-Path $weaviateDockerCompose) {
    Write-Host "📦 Starting services from docker-compose-weaviate.yml" -ForegroundColor Cyan
    docker-compose -f $weaviateDockerCompose up -d
    Write-Host "✅ Weaviate Docker services started" -ForegroundColor Green
} else {
    Write-Host "⚠️ docker-compose-weaviate.yml not found at $weaviateDockerCompose" -ForegroundColor Yellow
}

# Check database requirements
Write-Host ""
Write-Host "📋 Database Requirements:" -ForegroundColor Yellow
Write-Host "  - PostgreSQL must be running on localhost:55432" -ForegroundColor Yellow
Write-Host "  - Redis must be running on localhost:6379" -ForegroundColor Yellow
Write-Host ""

# Ask user if databases are ready
$response = Read-Host "Are PostgreSQL and Redis running? (y/N)"
if ($response -ne 'y' -and $response -ne 'Y') {
    Write-Host "Please start PostgreSQL and Redis first, then run this script again." -ForegroundColor Yellow
    Read-Host "Press Enter to exit"
    exit 1
}

Write-Host "🚀 Starting emobot services..." -ForegroundColor Green

# Start Celery worker in new window
Write-Host "📋 Starting Celery worker..." -ForegroundColor Cyan
Start-Process cmd -ArgumentList "/k", "uv run celery -A config.celery_config worker --loglevel=info --pool=solo" -WindowStyle Normal

# Start FastAPI web server in new window
Write-Host "🌐 Starting FastAPI web server..." -ForegroundColor Cyan
Start-Process cmd -ArgumentList "/k", "uv run uvicorn app:app --host 0.0.0.0 --port 8089 --reload --log-level debug" -WindowStyle Normal

# Start Cloud WebSocket server in new window
Write-Host "☁️ Starting Cloud WebSocket server..." -ForegroundColor Cyan
Start-Process cmd -ArgumentList "/k", "uv run cloud_websocket_server.py" -WindowStyle Normal

Write-Host ""
Write-Host "🎉 Emobot is starting!" -ForegroundColor Green
Write-Host ""
Write-Host "📋 Services:" -ForegroundColor Yellow
Write-Host "  - Web API: http://localhost:8089" -ForegroundColor Yellow
Write-Host "  - API Documentation: http://localhost:8089/docs" -ForegroundColor Yellow
Write-Host "  - WebSocket Server: ws://localhost:8088" -ForegroundColor Yellow
Write-Host ""
Write-Host "📝 Three new windows should open for Celery Worker, FastAPI Server, and WebSocket Server" -ForegroundColor Yellow
Write-Host "🛑 To stop services, close the three command windows" -ForegroundColor Yellow
Write-Host ""
Read-Host "Press Enter to exit"